-- HealerOptions.lua — Options du mode Healer pour Sleek Combat Text
-- Ajoute l’option “Afficher les absorptions” + Test complet.
local ADDON, DFFT = ...
if not DFFT or not DFFT.DB then return end
local DB  = DFFT.DB
DB.healer = DB.healer or {}
local HDB = DB.healer

-- ========== UI helpers ==========
local function mkPanel(name)
  local p = CreateFrame("Frame", nil, InterfaceOptionsFramePanelContainer or UIParent)
  p.name = name
  if InterfaceOptions_AddCategory then InterfaceOptions_AddCategory(p) end
  return p
end

local function addTitle(parent, text)
  local fs = parent:CreateFontString(nil, "ARTWORK", "GameFontHighlightLarge")
  fs:SetPoint("TOPLEFT", 16, -16)
  fs:SetText(text)
  return fs
end

local function addCheck(parent, label, tooltip, get, set)
  local b = CreateFrame("CheckButton", nil, parent, "InterfaceOptionsCheckButtonTemplate")
  b.Text:SetText(label)
  if tooltip then b.tooltipText = label; b.tooltipRequirement = tooltip end
  b:SetScript("OnShow", function(self)
    local ok, v = pcall(get)
    self:SetChecked(ok and v and true or false)
  end)
  b:SetScript("OnClick", function(self)
    pcall(set, self:GetChecked() and true or false)
  end)
  return b
end

local function addDropdown(parent, label, items, get, set)
  local f = CreateFrame("Frame", nil, parent, "UIDropDownMenuTemplate")
  local title = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
  title:SetText(label)
  title:SetPoint("BOTTOMLEFT", f, "TOPLEFT", 16, 0)
  UIDropDownMenu_SetWidth(f, 180)
  UIDropDownMenu_Initialize(f, function(self, level)
    local ok, selected = pcall(get); if not ok then selected = nil end
    for _,it in ipairs(items) do
      local info = UIDropDownMenu_CreateInfo()
      info.text = it.text
      info.func = function()
        pcall(set, it.value)
        UIDropDownMenu_SetText(f, it.text)
      end
      info.checked = (selected == it.value)
      UIDropDownMenu_AddButton(info, level)
    end
  end)
  f:SetScript("OnShow", function()
    local ok, sel = pcall(get)
    for _,it in ipairs(items) do
      if it.value == sel then UIDropDownMenu_SetText(f, it.text); break end
    end
  end)
  return f
end

local function addSlider(parent, label, minv, maxv, step, get, set, suffix)
  local s = CreateFrame("Slider", nil, parent, "OptionsSliderTemplate")
  s:SetMinMaxValues(minv, maxv)
  s:SetValueStep(step)
  s:SetObeyStepOnDrag(true)
  s:SetWidth(220)
  local textFS, lowFS, highFS = s.Text, s.Low, s.High

  local function fmt(vnum)
    vnum = tonumber(vnum)
    if vnum then vnum = math.floor(vnum + 0.5); return string.format("%s (%d%s)", label, vnum, suffix or "") end
    return label
  end

  s:SetScript("OnShow", function(self)
    local ok, v = pcall(get); v = tonumber(v) or minv
    self:SetValue(v)
    if textFS then textFS:SetText(fmt(v)) end
    if lowFS  then lowFS:SetText(tostring(minv)) end
    if highFS then highFS:SetText(tostring(maxv)) end
  end)
  s:SetScript("OnValueChanged", function(self, value)
    local v = tonumber(value) and math.floor(value + 0.5) or minv
    pcall(set, v)
    if textFS then textFS:SetText(fmt(v)) end
  end)
  if textFS then textFS:SetText(label) end
  if lowFS  then lowFS:SetText(tostring(minv)) end
  if highFS then highFS:SetText(tostring(maxv)) end
  return s
end

-- ========== Panel ==========
local panel = mkPanel("流畅战斗文本 – 治疗模式")
do
  local title = addTitle(panel, "流畅战斗文本 — 治疗模式")

  local modeDD = addDropdown(panel, "激活方式", {
    {text="自动（根据天赋）", value="AUTO"},
    {text="强制开启",              value="ON"},
    {text="强制关闭",             value="OFF"},
  }, function() return HDB.mode end,
     function(v) HDB.mode = v end)
  modeDD:SetPoint("TOPLEFT", title, "BOTTOMLEFT", -16, -12)

  local info = panel:CreateFontString(nil, "ARTWORK", "GameFontDisable")
  info:SetPoint("TOPLEFT", modeDD, "BOTTOMLEFT", 18, -10)
  info:SetText("锚点使用你的通用设置'在目标上显示数字'。没有单独的治疗模式锚点。")

  local win = addSlider(panel, "自动校准窗口（秒）", 30, 300, 10,
    function() return HDB.auto and HDB.auto.window end,
    function(v) HDB.auto = HDB.auto or {}; HDB.auto.window = v end, "s")
  win:SetPoint("TOPLEFT", info, "BOTTOMLEFT", -18, -18)

  local incHOT = addCheck(panel, "校准时包含HoT效果", nil,
    function() return HDB.auto and HDB.auto.includeHOT end,
    function(v) HDB.auto = HDB.auto or {}; HDB.auto.includeHOT = v end)
  incHOT:SetPoint("LEFT", win, "RIGHT", 40, 0)

  local heur = addCheck(panel, "使用启发式算法（近期最大值的百分比）而非百分位数", nil,
    function() return HDB.auto and HDB.auto.heuristic end,
    function(v) HDB.auto = HDB.auto or {}; HDB.auto.heuristic = v end)
  heur:SetPoint("TOPLEFT", win, "BOTTOMLEFT", 0, -10)

  -- Overheal
  local ohTitle = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
  ohTitle:SetPoint("TOPLEFT", heur, "BOTTOMLEFT", 0, -16)
  ohTitle:SetText("过量治疗")

  local showOH = addCheck(panel, "显示过量治疗后缀 (~xx% OH)", "为清晰起见，默认禁用。",
    function() return HDB.overheal and HDB.overheal.show end,
    function(v) HDB.overheal = HDB.overheal or {}; HDB.overheal.show = v end)
  showOH:SetPoint("TOPLEFT", ohTitle, "BOTTOMLEFT", -4, -6)

  local thr = addSlider(panel, "过量治疗阈值 (%)", 0, 100, 5,
    function() return HDB.overheal and HDB.overheal.threshold and (HDB.overheal.threshold * 100) end,
    function(v) HDB.overheal = HDB.overheal or {}; HDB.overheal.threshold = v/100 end, "%")
  thr:SetPoint("TOPLEFT", showOH, "BOTTOMLEFT", 0, -14)

  -- Absorptions
  local absTitle = panel:CreateFontString(nil, "ARTWORK", "GameFontNormal")
  absTitle:SetPoint("TOPLEFT", thr, "BOTTOMLEFT", 0, -16)
  absTitle:SetText("吸收")

  local showAbs = addCheck(panel, "显示吸收增益 (玩家/目标)", "使用 UNIT_ABSORB_AMOUNT_CHANGED 事件。",
    function() return HDB.absorbs and HDB.absorbs.show end,
    function(v) HDB.absorbs = HDB.absorbs or {}; HDB.absorbs.show = v end)
  showAbs:SetPoint("TOPLEFT", absTitle, "BOTTOMLEFT", -4, -6)

  local minAbs = addSlider(panel, "最小变化值 (吸收)", 1, 5000, 50,
    function() return HDB.absorbs and HDB.absorbs.minDelta end,
    function(v) HDB.absorbs = HDB.absorbs or {}; HDB.absorbs.minDelta = v end, "")
  minAbs:SetPoint("TOPLEFT", showAbs, "BOTTOMLEFT", 0, -14)

  -- Test button (showcase complet)
  local testBtn = CreateFrame("Button", nil, panel, "UIPanelButtonTemplate")
  testBtn:SetSize(240, 24); testBtn:SetText("Test affichage (Healer: soins + absorbs)")
  testBtn:SetPoint("TOPLEFT", minAbs, "BOTTOMLEFT", 0, -18)
  testBtn:SetScript("OnClick", function()
    if DFFT and DFFT.Healer and DFFT.Healer.TestHeals then DFFT.Healer.TestHeals() end
  end)
end
